module ApplicationHelper
  def is_active?(c_name, a_name = nil)
    active = false
    if c_name == controller_name
      if a_name != nil
        active = true if a_name == action_name
      else
        active = true
      end
    end
    active
  end

  def in_active?(c_names)
    c_names.include?(controller_name) ? true : false
  end

  def i18n_enum model, type
    I18n.t "#{model.class.model_name.i18n_key}.#{type}.#{model.send(type)}" if model.respond_to?(type)
  end

  def i18n_global var
    I18n.t "gloabl.#{var}"
  end

  def i18n_model var
    I18n.t "model.#{var}"
  end

  def i18n_action var
    I18n.t "action.#{var}"
  end

  def formattime(time, options = {})
    options[:unit]
    options[:unit] = options[:unit].blank? ? "day" : options[:unit]
    case options[:unit]
    when "day"
      time.to_datetime.strftime('%Y年%m月%d日') if time
    when "noYearOnlyDay"
      time.to_datetime.strftime('%m月%d日') if time
    when "noYear"
      time.to_datetime.strftime('%m月%d日 %H:%M') if time
    when "hour"
      time.to_datetime.strftime('%Y年%m月%d日 %H:%M') if time
    when "noDayOnlyHour"
      time.to_datetime.strftime('%H:%M') if time
    when "numberString"
      time.to_datetime.strftime('%Y%m%d%H%M') if time
    end
  end

  def user_avatar
    current_user.wechat_avatar.blank? ? 'avatar.jpg' : current_user.wechat_avatar
  end

  def notice_message
    flash_messages = []

    close_html = %(<button name="button" type="button" class="close" data-dismiss="alert"></button>)

    flash.each do |type, message|
      type = :success if type.to_sym == :notice
      type = :danger  if type.to_sym == :alert
      text = content_tag(:div, raw(close_html) + message, class: "alert alert-#{type}")
      flash_messages << text if message
    end

    flash_messages.join("\n").html_safe
  end

  def shop_categories_tree node_id = 0
    tree_node_html = ""
    if node_id == 0
      shop_categories = ShopCategory.where("parent_id = 0 OR parent_id IS NULL").order("sort ASC")
    else
      shop_categories = ShopCategory.where(parent_id: node_id).order("sort ASC")
    end
    shop_categories.each do |shop_category|
      if shop_category.children.count > 0
        tree_node_html += "<li class='leaf'>#{shop_category.name}</li>"
        tree_node_html += shop_categories_tree shop_category.id
      else
        if shop_category.level == 3
          tree_node_html += "<li class=''><a href='#{ new_shop_shop_category_spu_path(shop_category) }'>#{shop_category.name}</a></li>"
        else
          tree_node_html += "<li class='leaf'>#{shop_category.name}</li>"
        end
      end
    end
    tree_html = content_tag(:ul, raw(tree_node_html), class: "node #{'none' if node_id != 0}", style: "#{node_id != 0 ? 'margin-left:20px;' : 'list-style:none;'}")
  end

  def shop_categories_tree_in_index node_id = 0
    tree_node_html = ""
    if node_id == 0
      shop_categories = ShopCategory.where("parent_id = 0 OR parent_id IS NULL").order("sort ASC")
    else
      shop_categories = ShopCategory.where(parent_id: node_id).order("sort ASC")
    end
    shop_categories.each do |shop_category|
      if shop_category.children.count > 0
        tree_node_html += "<li class='leaf'>#{shop_category.name}</li>"
        tree_node_html += shop_categories_tree_in_index shop_category.id
      else
        if shop_category.level == 3
          tree_node_html += "<li class=''><a href='#{ shop_spus_path(shop_category_id: shop_category.id) }'>#{shop_category.name}</a></li>"
        else
          tree_node_html += "<li class='leaf'>#{shop_category.name}</li>"
        end
      end
    end
    tree_html = content_tag(:ul, raw(tree_node_html), class: "node #{'none' if node_id != 0}", style: "#{node_id != 0 ? 'margin-left:20px;' : 'list-style:none;'}")
  end

  def shop_categories_tree_in_admin node_id = 0
    tree_node_html = ""
    if node_id == 0
      shop_categories = ShopCategory.where("parent_id = 0 OR parent_id IS NULL").order("sort ASC")
    else
      shop_categories = ShopCategory.where(parent_id: node_id).order("sort ASC")
    end
    shop_categories.each do |shop_category|
      if shop_category.children.count > 0
        tree_node_html += "<li class='leaf'>#{shop_category.name}</li>"
        tree_node_html += shop_categories_tree_in_admin shop_category.id
      else
        if shop_category.level == 3
          tree_node_html += "<li class=''><a href='#{ admin_spus_path(shop_category_id: shop_category.id) }'>#{shop_category.name}</a></li>"
        else
          tree_node_html += "<li class='leaf'>#{shop_category.name}</li>"
        end
      end
    end
    tree_html = content_tag(:ul, raw(tree_node_html), class: "node #{'none' if node_id != 0}", style: "#{node_id != 0 ? 'margin-left:20px;' : 'list-style:none;'}")
  end

  def shop_categories_tree_in_active(active, node_id = 0)
    tree_node_html = ""
    if node_id == 0
      shop_categories = ShopCategory.where("parent_id = 0 OR parent_id IS NULL").order("sort ASC")
    else
      shop_categories = ShopCategory.where(parent_id: node_id).order("sort ASC")
    end
    shop_categories.each do |shop_category|
      if shop_category.children.count > 0
        tree_node_html += "<li class='leaf'>#{shop_category.name}</li>"
        tree_node_html += shop_categories_tree_in_active(active, shop_category.id)
      else
        if shop_category.level == 3
          tree_node_html += "<li class=''><a href='#{ admin_active_active_shop_categories_path(active, shop_category_id: shop_category.id) }' rel='nofollow' data-method='post'>#{shop_category.name}</a></li>"
        else
          tree_node_html += "<li class='leaf'>#{shop_category.name}</li>"
        end
      end
    end
    tree_html = content_tag(:ul, raw(tree_node_html), class: "node #{'none' if node_id != 0}", style: "#{node_id != 0 ? 'margin-left:20px;' : 'list-style:none;'}")
  end

  def shop_categories_list shop_categories, index = 1
    tree_node_html = ""
    shop_categories.each do |shop_category|
      tree_node_html += render partial: "body", locals: { shop_category: shop_category, index: index }
      tree_node_html += shop_categories_list(shop_category.children, index + 1) if shop_category.children.count > 0
    end
    raw(tree_node_html)
  end

  def spu_categories_list spu_categories, index = 1
    tree_node_html = ""
    spu_categories.each do |spu_category|
      tree_node_html += render partial: "body", locals: { spu_category: spu_category, index: index }
      tree_node_html += spu_categories_list(spu_category.children, index + 1) if spu_category.children.count > 0
    end
    raw(tree_node_html)
  end

  def channels_list channels, index = 1
    tree_node_html = ""
    channels.each do |channel|
      tree_node_html += render partial: "body", locals: { channel: channel, index: index }
      tree_node_html += channels_list(channel.children, index + 1) if channel.children.count > 0
    end
    raw(tree_node_html)
  end

  def has_image? url
    if url.include?('image_load.png')
      ''
    else
      url
    end
  end

  def computed_price price
    if @current_user.blank? || @current_user.membership.blank?
      price
    else
      price * @current_user.membership.discount / 100
    end
  end
  
end
